﻿
layui.use('table', function () {
    var table = layui.table;
    //方法级渲染
    table.render({
        elem: '#LAY_table_user'
        , url: '/Index/GetAllMenuList'
        , cols: [[
            { checkbox: true },
            { field: 'menuId', title: 'ID', width: 80, sort: true },
            { field: 'menuName', title: '菜单名', width: 120 },
            { field: 'menuUrl', title: '菜单路径', width: 200 },
            { field: 'menuParentId', title: '父级菜单id', width: 120 }
        ]]
        , id: 'testReload'
        , page: true
        , height: 630
    });

    //搜索
    var $ = layui.$, active = {
        reload: function () {
            var demoReload = $('#demoReload');
            table.reload('testReload', {
                where: {
                    MenuName: demoReload.val()
                }
            });
        }
    };
    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //新增
    $("div").delegate('#AddMenu', 'click', function () {
        layer.open({
            type: 2,
            title: "新增菜单",
            area: ['500px', '550px'],
            fixed: false, //不固定
            maxmin: true,
            shade: 0, //不遮挡
            id: 'addMenu',//防止重复弹出
            content: '/Index/AddMneu',
            success: function (layero, index) {
                //layer.full(index);  //打开时最大化
            }
        });
    });

    //编辑
    $("div").delegate('#EditMenu', 'click', function () {
        // 获取选中列数据
        var checkStatus = table.checkStatus('testReload');
        if (checkStatus.data.length === 0) {
            layer.alert("没有编辑信息");
            return;
        }
        layer.open({
            type: 2,
            title: "编辑菜单",
            area: ['500px', '550px'],
            fixed: false, //不固定
            maxmin: true,
            shade: 0, //不遮挡
            id: 'editMenu',//防止重复弹出
            content: '/Index/EditMenu?menuId=' + checkStatus.data[0].menuId,  // 只选择第一条
            success: function (layero, index) {
                //layer.full(index);  //打开时最大化
            }
        });
    });

    //删除
    $("div").delegate('#DelMenu', 'click', function () {
        // 获取选中列数据
        var checkStatus = table.checkStatus('testReload');
        if (checkStatus.data.length === 0) {
            layer.alert("没有删除信息");
            return;
        }
        //循环获得id
        var menuId = [];
        for (var i = 1; i <= checkStatus.data.length; i++) {
            menuId.push({ MenuId: checkStatus.data[i - 1].menuId });
        }
        layer.confirm('你确定要删除选中项？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                url: "/Index/DelMenu",
                type: 'post',
                data: { menuId: menuId },
                dataType: "json",
                async: "false",
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg("删除成功", {
                            time: 2000 //20s后自动关闭
                            , icon: 1
                        });
                        setTimeout('myrefresh()', 800); //隔一秒刷新
                    }
                    else {
                        layer.msg("删除失败", {
                            time: 2000 //20s后自动关闭
                            , icon: 5
                        });
                    }
                }
            });
        });
    });
});